package com.hctforgreen.greenservice.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hctforgreen.greenservice.model.VideoTypeEntity.VideoDetailEntity.VideoPartEntity;

/**
 * 
 * 视频观看记录
 * 
 * @author denghb 20141011
 * 
 */
public class DBVideoManager {
	private DBHelper helper;
	private SQLiteDatabase db;
	private static final String TABLE_VIDEO_PART = "table_video_part";

	public DBVideoManager(Context context) {
		helper = new DBHelper(context);
		// 因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0,
		// mFactory);
		// 所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里
		db = helper.getWritableDatabase();
	}

	/**
	 * add messages
	 * 
	 * @param partEntitys
	 */
	public void add(List<VideoPartEntity> data) {
		db.beginTransaction(); // 开始事务
		try {
			for (VideoPartEntity partEntity : data) {
				if (!existspartEntity(partEntity)) {
					db.execSQL("INSERT INTO " + TABLE_VIDEO_PART
							+ " VALUES(?, ?, ?, ?,?,?)", new Object[] {
							partEntity.id, partEntity.picUrl,
							partEntity.videoUrl, partEntity.videoName,
							partEntity.part });
				}
			}
			db.setTransactionSuccessful(); // 设置事务成功完成

		} catch (Exception e) {// 主键冲突异常
			e.printStackTrace();
		} finally {
			db.endTransaction(); // 结束事务
		}
	}

	/**
	 * add messages
	 * 
	 * @param partEntitys
	 */
	public void delete(List<VideoPartEntity> partEntitys) {
		db.beginTransaction(); // 开始事务
		try {
			for (VideoPartEntity partEntity : partEntitys) {
				db.execSQL("delete from " + TABLE_VIDEO_PART + " where id=? ",
						new Object[] { partEntity.id });

			}
			db.setTransactionSuccessful(); // 设置事务成功完成

		} catch (Exception e) {// 主键冲突异常
			e.printStackTrace();
		} finally {
			db.endTransaction(); // 结束事务
		}
	}

	private boolean existspartEntity(VideoPartEntity partEntity) {
		Cursor c = db.rawQuery("select id from " + TABLE_VIDEO_PART
				+ " where id=?", new String[] { partEntity.id });
		if (c == null || c.getCount() == 0) {
			// Log.w("ss", c.getCount());
			return false;
		}
		return c.getCount() > 0;

	}

	/**
	 * add videoPartEntity
	 * 
	 * @param partEntity
	 */
	public void add(VideoPartEntity partEntity) {
		if (existspartEntity(partEntity)) {
			return;
		}

		db.beginTransaction(); // 开始事务
		try {
			db.execSQL("INSERT INTO " + TABLE_VIDEO_PART
					+ " VALUES(?, ?, ?, ?, ?)", new Object[] { partEntity.id,
					partEntity.picUrl, partEntity.videoUrl,
					partEntity.videoName, partEntity.part });
			db.setTransactionSuccessful(); // 设置事务成功完成
		} finally {
			db.endTransaction(); // 结束事务
		}
	}

	public void updatepartEntity(VideoPartEntity partEntity) {
		db.beginTransaction(); // 开始事务
		try {
			db.execSQL("UPDATE " + TABLE_VIDEO_PART
					+ " picUrl=? , videoUrl=? , videoName=?, part=?"
					+ "where id=?", new Object[] { partEntity.picUrl,
					partEntity.videoUrl, partEntity.videoName, partEntity.part,
					partEntity.id });
			db.setTransactionSuccessful(); // 设置事务成功完成
		} finally {
			db.endTransaction(); // 结束事务
		}

	}

	/**
	 * delete old videoPartEntity
	 * 
	 * @param person
	 */
	public void deleteOldPwd(VideoPartEntity partEntity) {
		db.delete(TABLE_VIDEO_PART, "id = ?",
				new String[] { String.valueOf(partEntity.id) });
	}

	/**
	 * query all videoTypeEntities, return list
	 * 
	 * @return List<MessagesEntity>
	 */
	public List<VideoPartEntity> queryAll() {
		ArrayList<VideoPartEntity> partEntitys = new ArrayList<VideoPartEntity>();
		Cursor c = queryTheCursor();
		while (c.moveToNext()) {
			VideoPartEntity partEntity = new VideoPartEntity();
			partEntity.id = c.getString(c.getColumnIndex("id"));
			partEntity.picUrl = c.getString(c.getColumnIndex("picUrl"));
			partEntity.videoUrl = c.getString(c.getColumnIndex("videoUrl"));
			partEntity.videoName = c.getString(c.getColumnIndex("videoName"));
			partEntity.part = c.getString(c.getColumnIndex("part"));

			partEntitys.add(partEntity);
		}
		c.close();
		return partEntitys;
	}

	/**
	 * query all warns, return cursor
	 * 
	 * @return Cursor
	 */
	public Cursor queryTheCursor() {
		Cursor c = db.rawQuery("SELECT * FROM " + TABLE_VIDEO_PART, null);
		return c;
	}

	/**
	 * close database
	 */
	public void closeDB() {
		db.close();
	}
}
